package com.example.demo01;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btnHello;
    private MyButton mybtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btn4 = findViewById(R.id.btn_4);
        btnHello = findViewById(R.id.btn_hello);
        mybtn = findViewById(R.id.mybtn);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startAnimation0();
                //startAnimation1();
                //startAnimation2();
                startAnimation3();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                float curTranslationX = btnHello.getTranslationX();
                ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "translationX", curTranslationX, 600, curTranslationX);
                animator.setDuration(5000);
                animator.setRepeatCount(5);
                animator.setRepeatMode(ValueAnimator.RESTART);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        btnHello.setTranslationY(value);
                        btnHello.setText("Hello" + value);
                    }
                });
                animator.start();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "rotation", 0, 360);
                animator.setDuration(5000);
                animator.setRepeatCount(5);
                animator.setRepeatMode(ValueAnimator.RESTART);
                animator.start();
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "scaleX", 1, 3, 1);
                animator.setDuration(5000);
                animator.setRepeatCount(5);
                animator.setRepeatMode(ValueAnimator.RESTART);
                animator.start();
            }
        });

    }



    private void startAnimation3() {
        // 步骤1：设置需要组合的动画效果
        float curTranslationX = mybtn.getTranslationX();
        ObjectAnimator translation = ObjectAnimator.ofFloat(mybtn, "translationX", curTranslationX, 300,curTranslationX);
        // 平移动画
        ObjectAnimator rotate = ObjectAnimator.ofFloat(mybtn, "rotation", 0f, 360f);
        // 旋转动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mybtn, "alpha", 1f, 0f, 1f);
        // 透明度动画

        // 步骤2：创建组合动画的对象
        AnimatorSet animSet = new AnimatorSet();

        // 步骤3：根据需求组合动画
        animSet.play(translation).with(rotate).before(alpha);
        animSet.setDuration(5000);

        // 步骤4：启动动画
        animSet.start();
    }

    private void startAnimation2() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mybtn, "myText", 1, 1000, 1);
        objectAnimator.setDuration(3000);
        objectAnimator.setRepeatCount(3);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.start();
    }

    private void startAnimation0() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "alpha", 1, 0, 1);
        animator.setDuration(5000);
        animator.setRepeatCount(5);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.start();
    }


    private void startAnimation1() {
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_animator);
        objectAnimator.setTarget(btnHello);
        objectAnimator.start();
    }


}














